package com.qys.livesMall.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * Created by IntelliJ IDEA.
 * User: dp
 * Date: 2023/05/25
 * Time: 10:00
 * DESCRIPTION:
 */
@Configuration
@Profile({"dev", "test"})
public class SwaggerConfig {

    private static final String headerName = "Authorization";//请求头名称

    @Bean
    public GroupedOpenApi app() {
        return GroupedOpenApi.builder()
                .group("APP")
                .addOperationCustomizer((operation, handlerMethod) -> {
                    operation.addSecurityItem(new SecurityRequirement().addList(headerName));
                    return operation;
                })
                .packagesToScan(
                        "com.qys.livesMall.user.source.controller.app",
                        "com.qys.livesMall.app.user.controller.app",
                        "com.qys.livesMall.shortVideo.controller.app",
                        "com.qys.livesMall.security.controller",
                        "com.qys.livesMall.system.controller.app",
                        "com.qys.livesMall.system.source.controller",
                        "com.qys.livesMall.shop.controller.app",
                        "com.qys.livesMall.financial.controller.app",
                        "com.qys.livesMall.anchor.controller.app",
                        "com.qys.livesMall.open.controller.app"
                )
                .build();
    }

    @Bean
    public GroupedOpenApi System() {
        return GroupedOpenApi.builder()
                .group("后台管理")
                .addOperationCustomizer((operation, handlerMethod) -> {
                    operation.addSecurityItem(new SecurityRequirement().addList(headerName));
                    return operation;
                })
                .packagesToScan(
                        "com.qys.livesMall.user.source.controller.sys",
                        "com.qys.livesMall.app.user.controller.sys",
                        "com.qys.livesMall.shortVideo.controller.sys",
                        "com.qys.livesMall.security.controller",
                        "com.qys.livesMall.product.controller",
                        "com.qys.livesMall.postage.controller",
                        "com.qys.livesMall.system.controller.sys",
                        "com.qys.livesMall.system.source.controller",
                        "com.qys.livesMall.shop.controller.web",
                        "com.qys.livesMall.controller.sys",
                        "com.qys.livesMall.financial.controller.sys",
                        "com.qys.livesMall.shop.controller.sys",
                        "com.qys.livesMall.anchor.controller.sys",
                        "com.qys.livesMall.navy.controller.sys",
                        "com.qys.livesMall.quartz.controller.sys"
                )
                .build();
    }

    @Bean
    public GroupedOpenApi common() {
        return GroupedOpenApi.builder()
                .group("通用接口")
                .addOperationCustomizer((operation, handlerMethod) -> {
                    operation.addSecurityItem(new SecurityRequirement().addList(headerName));
                    return operation;
                })
                .packagesToScan(
                        "com.qys.livesMall.file.server.upload.controller"
                )
                .build();
    }

    @Bean
    public OpenAPI customOpenAPI() {
        Components components = new Components();
        //添加右上角的统一安全认证
        components.addSecuritySchemes(headerName,
                new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .scheme("basic")
                        .name(headerName)
                        .in(SecurityScheme.In.HEADER)
                        .description("请求头")
        );
        return new OpenAPI()
                .components(components)
                .info(apiInfo());
    }

    private Info apiInfo() {
        Contact contact = new Contact();
        contact.setEmail("");
        contact.setName("");
        contact.setUrl("https://www.baidu.com");
        return new Info()
                .title("直播商城")
                .version("1.0")
                .contact(contact)
                .description("https://www.baidu.com")
                .license(new License().name("Apache 2.0").url("http://springdoc.org"));
    }
}
